import {createRouter,createWebHashHistory,createWebHistory} from 'vue-router'
import Home from '../views/Home.vue'
import AdminEbook from '../views/admin/admin-ebook.vue'
import AdminCategory from '../views/admin/admin-category.vue'
import hello from '../components/Hello.vue'
import AdminDoc from '../views/admin/admin-doc.vue'
import User from '../views/admin/admin-user.vue'
import { nextTick } from '@vue/runtime-core'
import Tool from "../util/tool"
import store from '../store'
const routes=[
    {
        path:'/',
        name:'home',
        component:Home
    },{
        path:'/admin/user',
        name:'User',
        component:User,
        meta:{
            loginRequire:true
        }
    },{
        path:'/admin/ebook',
        name:'adminEbook',
        component:AdminEbook,
        meta:{
            loginRequire:true
        }
    },{
        path:'/admin/category',
        name:'adminCategory',
        component:AdminCategory,
        meta:{
            loginRequire:true
        }
    },{
        path:'/hello',
        name:'hello',
        component:hello  
    },
    {
        path:'/admin/doc',
        name:'AdminDoc',
        component:AdminDoc,
        meta:{
            loginRequire:true
        }  
    }

]
const router=createRouter({
    history:createWebHistory(),
    routes
})


//路由登录拦截
router.beforeEach((to,from,next)=>{
    //要不要对meta,loginRequire属性做监控拦截
    if(to.matched.some(function (item) {
        console.log(item,"是否需要登录校验：",item.meta.loginRequire);
        return item.meta.loginRequire;
    })){
        const loginUser=store.state.user;
        if(Tool.isEmpty(loginUser)){
            console.log("用户未登录！");
            next('/');
        }else{
            next();
        }
    }else{
        next();
    }
})

export default router